package com.yoomiito.app.ui.anyview.college

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ResUtil

/**
 * 商学院--目录view
 */
class SubjectTableView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    private lateinit var tv1: TextView
    private lateinit var tv2: TextView

    init {
        initView()
    }

    private fun initView() {

        tv1 = TextView(context)
        tv1.text = "视频"
        tv1.textSize = 10f
        tv1.gravity = Gravity.CENTER
        tv1.setPadding(DensityUtil.dp2px(6f), DensityUtil.dp2px(3f), DensityUtil.dp2px(6f), DensityUtil.dp2px(3f))
        val lp1 = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        val gd = GradientDrawable()
        gd.cornerRadius = DensityUtil.dp2px(4f).toFloat()
        gd.setColor(Color.WHITE)
        gd.setStroke(DensityUtil.dp2px(1f), ResUtil.getColor(R.color.color_gray_CCCCCC))
        tv1.background = gd
        addView(tv1, lp1)

        tv2 = TextView(context)
        tv2.textSize = 15f
        tv2.minHeight = DensityUtil.dp2px(24f)
        tv2.gravity = Gravity.CENTER_VERTICAL
        val lp2 = LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f)
        lp2.leftMargin = DensityUtil.dp2px(10f)
        addView(tv2, lp2)
    }

    fun setText(text:String){
        tv2.text = text
    }

    fun select(isSelected: Boolean) {
        val textColor = if (isSelected) ResUtil.getColor(R.color.color_orange) else ResUtil.getColor(R.color.color_balk_333333)
        tv1.setTextColor(textColor)
        tv2.setTextColor(textColor)

        val backGroundColor = if (isSelected) R.color.color_orange else R.color.color_gray_CCCCCC
        val gd = tv1.background as GradientDrawable
        gd.setStroke(DensityUtil.dp2px(1f), ResUtil.getColor(backGroundColor))
        tv1.background = gd
    }
}